import Vue from 'vue'
import Router from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Register from '../components/Register'
import Users from '../components/role/Users.vue'
import Error404 from '../components/Error404'
import welcome from '../components/welcome'
import Changepassword from '../components/role/Changepassword.vue'
Vue.use(Router);
const router = new Router({
    routes: [
        {
            path:'/login',
            name: 'login',
            component:Login,
        },
        {
            path: '/',
            redirect: '/login'
        },
        {
            path:'/home',
            component:Home,
            children:
                [
                    {
                        path:'users',
                        component:Users
                    },
                    {
                        path:'welcome',
                        component:welcome
                    },
                    {
                        path:'changepassword',
                        component:Changepassword
                    },
                ]
            
        },
        {
            path:'/register',
            component:Register,
        },
        {
            // *号表示剩余的全部情况
            path:'*',
            component:Error404,
        },
       
    ]
})
//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
    if(to.path==='/register')return next()
    if(to.path==='/login')return next()
    const tokenStr=window.sessionStorage.getItem('token')
    if(!tokenStr)return next('/login')
    next()
})
export default router